from color import *


class BaseCell:
    def __init__(self, board, rowNum, colNum):
        self.board = board
        self.canvas = board.canvas
        # self.cell = board.grid.rows[rowNum][colNum]
        self.x0 = colNum * self.board.cellSize + self.board.margin
        self.y0 = rowNum * self.board.cellSize + self.board.margin

        self.generate()

    def generateBlock(self):
        self.block = self.canvas.create_rectangle(
            self.x0,
            self.y0,
            self.x0 + self.board.cellSize,
            self.y0 + self.board.cellSize,
            fill="white",
            outline="",
            state="hidden",
        )

    def generateValueCircle(self):
        self.valueCircle = self.canvas.create_oval(
            self.x0 + int(self.board.cellSize * 0.1),
            self.y0 + int(self.board.cellSize * 0.1),
            self.x0 + self.board.cellSize - int(self.board.cellSize * 0.1),
            self.y0 + self.board.cellSize - int(self.board.cellSize * 0.1),
            fill="white",
            outline="",
            state="hidden",
        )

    def generateCandCircles(self):
        self.candCircles = []
        for i in range(9):
            c = self.canvas.create_oval(
                self.x0 + i % 3 * self.board.cellSize // 3 + 1,
                self.y0 + i // 3 * self.board.cellSize // 3 + 1,
                self.x0 + (i % 3 + 1) * self.board.cellSize // 3 - 1,
                self.y0 + (i // 3 + 1) * self.board.cellSize // 3 - 1,
                fill="white",
                outline="",
                state="hidden",
            )
            self.candCircles.append(c)

    def generateValue(self):
        x = self.x0 + self.board.cellSize // 2
        y = self.y0 + self.board.cellSize // 2
        self.textValue = self.canvas.create_text(
            (x, y),
            text="0",
            fill="black",
            font=self.board.valueFont,
            state="hidden",
        )

    def generateCands(self):
        self.textCands = []
        for i in range(9):
            x = self.x0 + i % 3 * self.board.cellSize // 3 + self.board.cellSize // 6
            y = self.y0 + i // 3 * self.board.cellSize // 3 + self.board.cellSize // 6
            c = self.canvas.create_text(
                (x, y),
                text=str(i + 1),
                font=self.board.candFont,
                fill="black",
                state="hidden",
            )
            self.textCands.append(c)

    def generate(self):
        self.generateBlock()
        # self.board.canvas.itemconfig(self.block, fill="orange", state="normal")
        self.generateValueCircle()
        self.generateCandCircles()
        self.generateValue()
        self.generateCands()

    def setValue(self, value, given=False):
        for c in self.textCands:
            self.canvas.itemconfig(c, state="hidden")
        if given:
            color = givenColor
        else:
            color = "black"
        self.canvas.itemconfig(
            self.textValue, text=str(value), fill=color, state="normal"
        )

    def setCands(self, cands):
        self.canvas.itemconfig(self.textValue, state="hidden")
        for i, c in enumerate(self.textCands, start=1):
            if i in cands:
                self.canvas.itemconfig(c, state="normal")
            else:
                self.canvas.itemconfig(c, state="hidden")


class Puzzle:
    pass
